package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.service.impl.CategoryServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@RestController
@Api("套餐管理")
public class setMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryServiceImpl categoryService;
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询")
    public Result<SetmealVO> queryById(@PathVariable Long id) {
        SetmealVO setmealVO = setMealService.queryById(id);
        return Result.success(setmealVO);
    }

    //新增套餐
    @PostMapping()
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames="setmealCache", allEntries=true)
    public Result<String> addMeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.addMeal(setmealDTO);
        return Result.success("新增套餐成功");
    }

    //分页查询
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO ) {
        PageResult pageResult = setMealService.pagequery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //修改套餐
    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames="setmealCache", allEntries=true)
    public Result<String> updateMeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.updateMeal(setmealDTO);
        return Result.success("修改套餐成功");
    }

    //套餐起售和停售
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐起售和停售")
    @CacheEvict(cacheNames="setmealCache", allEntries=true)
    public Result<String> updateStatus(@PathVariable Integer status, @RequestParam Long id) {
        setMealService.updatestatus(status, id);
        return Result.success("修改状态成功");
    }

    //批量删除套餐
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result<String> deleteMeal(@RequestParam List<Long> ids) {
        setMealService.deleteMeal(ids);
        return Result.success("删除成功");
    }
}
